package com.ruoyi.dynamic.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.dynamic.domain.DynamicModule;
import com.ruoyi.dynamic.service.IDynamicModuleService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 动态表格Controller
 * 
 * @author ningtao
 * @date 2022-11-27
 */
@Controller
@RequestMapping("/dynamic/module")
public class DynamicModuleController extends BaseController
{
    private String prefix = "dynamic/module";

    @Autowired
    private IDynamicModuleService dynamicModuleService;

    @RequiresPermissions("dynamic:module:view")
    @GetMapping()
    public String module()
    {
        return prefix + "/module";
    }

    /**
     * 查询动态表格列表
     */
    @RequiresPermissions("dynamic:module:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(DynamicModule dynamicModule)
    {
        startPage();
        List<DynamicModule> list = dynamicModuleService.selectDynamicModuleList(dynamicModule);
        return getDataTable(list);
    }

    /**
     * 导出动态表格列表
     */
    @RequiresPermissions("dynamic:module:export")
    @Log(title = "动态表格", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(DynamicModule dynamicModule)
    {
        List<DynamicModule> list = dynamicModuleService.selectDynamicModuleList(dynamicModule);
        ExcelUtil<DynamicModule> util = new ExcelUtil<DynamicModule>(DynamicModule.class);
        return util.exportExcel(list, "动态表格数据");
    }

    /**
     * 新增动态表格
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存动态表格
     */
    @RequiresPermissions("dynamic:module:add")
    @Log(title = "动态表格", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(DynamicModule dynamicModule)
    {
        return toAjax(dynamicModuleService.insertDynamicModule(dynamicModule));
    }

    /**
     * 修改动态表格
     */
    @RequiresPermissions("dynamic:module:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        DynamicModule dynamicModule = dynamicModuleService.selectDynamicModuleById(id);
        mmap.put("dynamicModule", dynamicModule);
        return prefix + "/edit";
    }

    /**
     * 修改保存动态表格
     */
    @RequiresPermissions("dynamic:module:edit")
    @Log(title = "动态表格", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(DynamicModule dynamicModule)
    {
        return toAjax(dynamicModuleService.updateDynamicModule(dynamicModule));
    }

    /**
     * 删除动态表格
     */
    @RequiresPermissions("dynamic:module:remove")
    @Log(title = "动态表格", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(dynamicModuleService.deleteDynamicModuleByIds(ids));
    }
}
